<template>
  <section class="post-list">
    <el-table :data="tableData" stripe height="calc(100vh - 215px)">
      <el-table-column prop="title" label="标题" width="350">
      </el-table-column>
      <el-table-column label="发布日期" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span>{{scope.row.created_at|dateFormat('YYYY-MM-DD hh:mm:ss')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新日期" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span>{{scope.row.updated_at|dateFormat('YYYY-MM-DD hh:mm:ss')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="view(scope.row)"
            type="text"
            size="small">
            查看详情<i class="el-icon-d-arrow-right"></i>
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      @current-change="listPage"
      :total="total">
    </el-pagination>
  </section>
</template>

<script>
import {PostService} from '@/service/api'

export default {
  name: 'PostList',
  data () {
    return {
      total: 0,
      tableData: []
    }
  },
  mounted: function () {
    this.listPage(1)
  },
  watch: {
    '$route': 'listPage'
  },
  methods: {
    listPage: function (val) {
      PostService.page(val, 5, this.$route.params.module)
        .then(res => {
          if (res) {
            this.tableData = res.posts
            this.total = res.total
          }
        })
    },
    view: function (item) {
      this.$router.push({path: '/post/view/' + item.id})
    }
  }
}
</script>

<style scoped>
</style>
